package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author Andrew Tolver
	 */
	public class ConveyorBelt extends FlxSprite
	{
		private var _beltSpeed:Number;
		
		/**
		 * Constructor for the conveyor belt object
		 * 
		 * @param	X			X position of the object
		 * @param	Y			Y position of the object
		 * @param	objectPiece	1 = Left belt piece, 2 = Center belt piece, 3 = Right belt piece
		 */
		public function ConveyorBelt(X:Number = 0, Y:Number = 0, objectPiece:Number = 2) 
		{			
			super(X, Y);
			switch(objectPiece)
			{
			case 1:
				loadGraphic(ImageFiles.beltLeft, false, false, 15, 15);
				break;
			case 2:
				loadGraphic(ImageFiles.beltCenter, true, false, 15, 15);
				break;
			case 3:				
				loadGraphic(ImageFiles.beltRight, false, false, 15, 15);
				break;
			}
			this.fixed = true;
			addAnimation("cycleLeft", [0, 1, 2], 15);
			addAnimation("cycleRight", [2, 1, 0], 15);
		}
		
		/**
		 * Set the speed for the moving object
		 * 
		 * @param	Speed		The speed you want the character to move
		 */
		public function setBeltSpeed(Speed:Number):void
		{
			_beltSpeed = Speed;
			if (Speed < 0)
			{
				play("cycleLeft");
			}
			else if (Speed > 0)
			{
				play("cycleRight");
			}
		}
		
		//Moves any object colliding with top of conveyor belt by the belt's speed
		override public function hitTop(Contact:FlxObject, Velocity:Number):void
		{
			Contact.x += _beltSpeed * FlxG.elapsed;
			
			//provide a false velocity so the character will still collide with map objects
			if (Contact.velocity.x == 0)
			{
				if (_beltSpeed > 0)
				{
					Contact.velocity.x = 0.01;
				}
				else
				{
					Contact.velocity.x = -0.01;
				}
			}
			
			//adjust to prevent collision bug
			if ((FlxG.keys.justPressed("A") || FlxG.keys.justPressed("LEFT")) && _beltSpeed > 0)
			{
				Contact.velocity.x = -_beltSpeed;
			}
			if ((FlxG.keys.justPressed("D") || FlxG.keys.justPressed("RIGHT")) && _beltSpeed < 0)
			{
				Contact.velocity.x = -_beltSpeed;
			}
			
		}
	}

}